package com.game.spaceship;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import util.Constants;
import util.EnemyFactory;
import util.EnemyListSingleton;
import util.EnemyLoader;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.FPSLogger;
import com.badlogic.gdx.graphics.GLTexture;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

import field.objects.PlayerShip;
import field.objects.Ship;
import field.objects.Weapon;
import field.objects.enemies.Scout;

public class SpaceShipGame implements ApplicationListener, Constants, Observer {
	// private OrthographicCamera camera;
	private SpriteBatch batch;
	private Texture bgTexture;
	private Sprite bgSprite;
	private Sprite bgSprite2;
	public static float currentBgY;
	public static long lastTimeBg;
	public Ship player;
	public List<Weapon> weaponList = new ArrayList<Weapon>();
	FPSLogger log;

	@Override
	public void create() {
		GLTexture.setEnforcePotImages(false);
		log = new FPSLogger();
		batch = new SpriteBatch();
		// float w = Gdx.graphics.getWidth();
		// float h = Gdx.graphics.getHeight();
		// camera = new OrthographicCamera(1, h / w);
		loadBackground();
		Music mp3Music = Gdx.audio.newMusic(Gdx.files.internal("data/music/theme.mp3"));
		mp3Music.play();
		List<Ship> enemyMap = new ArrayList<Ship>();
		for (int i = 0; i < 5; i++) {
			enemyMap.add(EnemyFactory.getEnemy(Scout.class, null));
		}
		EnemyLoader loader = new EnemyLoader(enemyMap, EnemyFactory.getEnemy(Scout.class, null), 3000);
		Thread t = new Thread(loader, "My Thread");
		loader.addObserver(this);
		t.start();
		// loadShip();
		player = new PlayerShip(weaponList);
		// bgTexture = new Texture(Gdx.files.internal("data/background/1.png"));
		// bgTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		// bgSprite = new Sprite(bgTexture, 0, 0, WIDTH, HEIGHT);
		// st = new ShipThrusters(batch);
		// st.load();
		// ScaleParticle(st.getEffect(), 0.1f);
	}

	@Override
	public void dispose() {
		batch.dispose();
		bgTexture.dispose();
		player.dispose();
		for (Ship enemy : getEnemies()) {
			enemy.dispose();
		}
	}

	private List<Ship> getEnemies() {
		return EnemyListSingleton.getEnemyList().getEnemies();
	}

	@Override
	public void render() {
		// Gdx.gl.glClearColor(1, 1, 1, 1);
		// Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		log.log();
		// batch.setProjectionMatrix(camera.combined);
		batch.begin();
		renderBackground();
		player.render(batch);
		// st.render(Gdx.graphics.getDeltaTime());
		for (Ship enemy : getEnemies()) {
			enemy.render(batch);
		}
		batch.end();
	}

	@Override
	public void resize(int width, int height) {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}

	// private void loadWeapon() {
	// if (weaponList != null && !weaponList.isEmpty()) {
	// Weapon lastBullet = weaponList.get(weaponList.size() - 1);
	// }
	//
	// }
	private void loadBackground() {
		bgTexture = new Texture(Gdx.files.internal("data/background/1.png"));
		bgTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		bgSprite = new Sprite(bgTexture);
		bgTexture = new Texture(Gdx.files.internal("data/background/2.png"));
		bgTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		bgSprite2 = new Sprite(bgTexture);
		bgSprite.setSize(WIDTH, HEIGHT);
		bgSprite2.setSize(WIDTH, HEIGHT);
		bgSprite2.setPosition(0, HEIGHT);
	}

	private void renderBackground() {
		bgSprite.translateY(-.5f);
		bgSprite.draw(batch);
		bgSprite2.translateY(-.5f);
		bgSprite2.draw(batch);
		if (bgSprite.getY() <= -HEIGHT)
			bgSprite.setPosition(0, HEIGHT);
		if (bgSprite2.getY() <= -HEIGHT)
			bgSprite2.setPosition(0, HEIGHT);
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		System.out.println("Bittiiiiii");
	}
}
